﻿/************************************************************************

   Extended Silverlight Toolkit

   Copyright (C) 2010-2012 Xceed Software Inc.

   This program is provided to you under the terms of the Microsoft Public
   License (Ms-PL) as published at http://extendedsilverlight.codeplex.com/license

   Please purchase a commercial version of this toolkit if you are using
   it in a commercial product. The commercial versions support the project,
   and also include more features for each control, priority support,
   source code, updates, removed watermark, and a proprietary license.

   Visit http://xceed.com and follow @datagrid on Twitter.

  **********************************************************************/

using System;
using System.Diagnostics;
using System.ComponentModel;
using System.Collections.Generic;

namespace Xceed.Silverlight.Data
{
  [DebuggerStepThrough]
  internal abstract class GroupParentDataHelper : IDataHelper, IGroupDataHelper
  {
    #region Constructor

    protected GroupParentDataHelper( IGroupDataHelper childDataHelper )
    {
      if( childDataHelper == null )
        throw new ArgumentNullException( "childDataHelper" );

      m_childDataHelper = childDataHelper;
    }

    #endregion

    #region ChildDataHelper Propected Property

    protected IGroupDataHelper ChildDataHelper
    {
      get
      {
        return m_childDataHelper;
      }
    }

    private readonly IGroupDataHelper m_childDataHelper;

    #endregion

    #region IGroupHelper Members

    public virtual bool IsGroupPath( DataPath item )
    {
      DataPath childItem = this.ConvertBack( item );

      return m_childDataHelper.IsGroupPath( childItem );
    }

    public virtual DataPath GetGroupPath( DataPath item )
    {
      DataPath childItem = this.ConvertBack( item );

      return m_childDataHelper.GetGroupPath( childItem );
    }

    public virtual SortPosition CreateSortPosition( DataPath item, StartPosition startPosition )
    {
      DataPath childItem = this.ConvertBack( item );

      return m_childDataHelper.CreateSortPosition( childItem, startPosition );
    }

    #endregion

    #region IItemHelper Members

    public virtual object GetMemberValue(
      string propertyPath,
      DataPath item )
    {
      DataPath childItem = this.ConvertBack( item );

      return m_childDataHelper.GetMemberValue( propertyPath, childItem );
    }

    public virtual bool TryGetMemberValue(
      string propertyPath,
      DataPath item,
      out object value )
    {
      DataPath childItem = this.ConvertBack( item );

      return m_childDataHelper.TryGetMemberValue( propertyPath, childItem, out value );
    }

    #endregion

    #region IConvertHelper Members

    public virtual DataPath Convert( DataPath value )
    {
      return value;
    }

    public virtual DataPath ConvertBack( DataPath value )
    {
      return value;
    }

    public virtual DataPath DeepConvert( object value )
    {
      DataPath childItem = m_childDataHelper.DeepConvert( value );

      return this.Convert( childItem );
    }

    public virtual object DeepConvertBack( DataPath value )
    {
      DataPath childItem = this.ConvertBack( value );

      return m_childDataHelper.DeepConvertBack( childItem );
    }

    #endregion

    #region ICompareHelper Members

    public virtual int Compare( DataPath firstItem, DataPath secondItem )
    {
      DataPath firstChildItem = this.ConvertBack( firstItem );
      DataPath secondChildItem = this.ConvertBack( secondItem );

      return m_childDataHelper.Compare( firstChildItem, secondChildItem );
    }

    public virtual int Compare( DataPath item, SourcePosition position )
    {
      if( position.IsBeforeFirst )
        return 1;

      if( position.IsAfterLast )
        return -1;

      ItemPosition itemPosition = position as ItemPosition;
      if( itemPosition != null )
        return this.Compare( item, ( DataPath )itemPosition.Item );

      DataPath childItem = this.ConvertBack( item );

      return m_childDataHelper.Compare( childItem, position );
    }

    public int Compare( SourcePosition position, DataPath item )
    {
      return -this.Compare( item, position );
    }

    public int Compare( SourcePosition firstPosition, SourcePosition secondPosition )
    {
      if( object.ReferenceEquals( firstPosition, secondPosition ) )
        return 0;

      if( firstPosition.IsBeforeFirst )
        return -1;

      if( firstPosition.IsAfterLast )
        return 1;

      ItemPosition firstItemPosition = firstPosition as ItemPosition;
      if( firstItemPosition != null )
        return this.Compare( ( DataPath )firstItemPosition.Item, secondPosition );

      ItemPosition secondItemPosition = secondPosition as ItemPosition;
      if( secondItemPosition != null )
        return this.Compare( firstPosition, ( DataPath )secondItemPosition.Item );

      return m_childDataHelper.Compare( firstPosition, secondPosition );
    }

    public virtual int Compare( IList<SortDescription> sortDescriptions, DataPath firstItem, DataPath secondItem )
    {
      DataPath firstChildItem = this.ConvertBack( firstItem );
      DataPath secondChildItem = this.ConvertBack( secondItem );

      return m_childDataHelper.Compare( sortDescriptions, firstChildItem, secondChildItem );
    }

    public int Compare( string propertyPath, object firstValue, object secondValue )
    {
      return m_childDataHelper.Compare( propertyPath, firstValue, secondValue );
    }

    #endregion

    #region IEditHelper Members

    public virtual void Invalidate( DataPath item )
    {
      DataPath childItem = this.ConvertBack( item );

      m_childDataHelper.Invalidate( childItem );
    }

    #endregion

    #region IDataHelper Members

    public IEnumerable<SortDescription> GetCurrentCompleteSortOrder()
    {
      return m_childDataHelper.GetCurrentCompleteSortOrder();
    }

    #endregion
  }
}
